package java6703.lesson03;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Scanner;

public class LastDay6703 {
    public static void main(String[] args) {
        System.out.println("计算某月的最后一天");
        System.out.print("请按yyyy-mm的格式输入：");
        Scanner scanner = new Scanner(System.in);

        //去除输入的所有空格
        String date = scanner.nextLine().replace(" ", "");

        //调用getLastDay6703（）方法
        System.out.println(getLastDay6703(date));
    }


    /**
     * (1)将字符串日期转成整型存放置到date1日期中；
     * (2)指定一个计算规则；
     * (3)按制定的规则进行计算;
     * @param date
     * @return
     */
    public static String getLastDay6703(String date){
        try {
            String[] newString = date.split("-");
            if (newString[0].length() != 4 || newString[1].length() != 2) {
                return "请输入正确年份格式！";
            }

            //将字符串日期转成整型存放置到date1日期中；
            LocalDate date1 = LocalDate.of(Integer.parseInt(newString[0]), Integer.parseInt(newString[1]), 10);

            //计算规则
            TemporalAdjuster adjuster = TemporalAdjusters.lastDayOfMonth();

            //按规则计算
            LocalDate dateNew = date1.with(adjuster);
            return "本月的最后一天是" +
                    dateNew.toString().substring(dateNew.toString().length() - 2, dateNew.toString().length()) + "日";
        } catch (Exception x) {
            return "请输入正确年份格式";
        }
    }
}
